<?php
class MessagesController extends AppController {

	var $name = 'Messages';
        public function  beforeFilter() {
            parent::beforeFilter();
            $this->setLayout();
            $this->Auth->allow('*');
        }
        function index() {
		$this->Message->recursive = 0;
                $this->paginate = array(
                        'conditions' => array(
                            'Message.receiver_id' => $this->Bayon->userId(),
                        ),
                        'limit' => 5,
                        'order' => 'Message.id DESC'
                    );
                $messages = $this->paginate();
                $this->loadModel('User');
                $this->loadModel('Avatar');
                $i = 0;
                $temp = array();
                foreach($messages as $m){
                    $user = $this->User->getUser($m['Message']['sender_id']);
                    $temp[$i] = $m;
                    $temp[$i]['User'] = $user['User'];
                    $temp[$i]['avatar'] = $this->Avatar->getAvatar($m['Message']['sender_id'], AVATAR_WIDHT_48);
                    $i++;
                }
		$this->set('messages', $temp);

                //get friend 
                $this->loadModel('Friend');
                $friends = $this->Friend->getFriends($this->Bayon->userId());
                $i = 0;
                $sentTo = array();
                //pr($friends); die();
                foreach ($friends as $friend){
                    $sentTo[] = array('user_id' => $friend['Friend']['following'], 'username' => $this->User->getUsername($friend['Friend']['following']));
                }
               $this->set('sentTo', $sentTo);
	}
        function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for message', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Message->delete($id)) {
			$this->Session->setFlash(__('Message deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Message was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
        
        public function  reply($replyOf, $receiverId){
            if(empty($this->data)){
              $this->loadModel('User');
              $this->set('username', $this->User->getUsername($receiverId));
    
              $this->set('replyOf', $replyOf);
              $this->set('receiverId', $receiverId);
            }
        }

        public function messageSent(){
            $this->Message->recursive = 0;
                $this->paginate = array(
                        'conditions' => array(
                            'Message.sender_id' => $this->Bayon->userId(),
                        ),
                        'limit' => 5,
                        'order' => 'Message.id DESC'
                    );
                $messages = $this->paginate();
                $this->loadModel('User');
                $this->loadModel('Avatar');
                $i = 0;
                $temp = array();
                foreach($messages as $m){
                    $user = $this->User->getUser($m['Message']['receiver_id']);
                    $temp[$i] = $m;
                    $temp[$i]['User'] = $user['User'];
                    $temp[$i]['avatar'] = $this->Avatar->getAvatar($m['Message']['receiver_id'], AVATAR_WIDHT_48);
                    $i++;
                }
		$this->set('messages', $temp);
        }
        public function  sent(){
            if(!empty($this->data)){
                $this->Message->create();
                $this->data['Message']['sender_id'] = $this->Bayon->userId();
                $this->data['Message']['date_sent'] = date('Y-d-m h:i:s');
                if($this->Message->save($this->data)){
                     //load user Modle
                    $this->loadModel('User');
                     $user = $this->User->find('first', array('conditions' => array('User.id' => $this->data['Message']['receiver_id'])));
                     $this->Session->setFlash(sprintf(__('Your meessage has been sent to %s.', true), $user['User']['username']));
                     
                     $this->redirect(array('action' => 'index'));
                }
            }else{
                $this->Session->setFlash(__('Cannot sent message please try again.', true));
            }
        }
        
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(sprintf(__('Invalid %s', true), 'message'));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('message', $this->Message->read(null, $id));
	}
/*
	function add() {
		if (!empty($this->data)) {
			$this->Message->create();
			if ($this->Message->save($this->data)) {
				$this->Session->setFlash(sprintf(__('The %s has been saved', true), 'message'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(sprintf(__('The %s could not be saved. Please, try again.', true), 'message'));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(sprintf(__('Invalid %s', true), 'message'));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Message->save($this->data)) {
				$this->Session->setFlash(sprintf(__('The %s has been saved', true), 'message'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(sprintf(__('The %s could not be saved. Please, try again.', true), 'message'));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Message->read(null, $id);
		}
	}
 *
 */
     	
        
}
?>